<?php
namespace app\qsapi\controller;

use app\common\controller\IdcardAuth;
use app\qsapi\service\AuthorityService;
use app\qsapi\service\MemberService;
use Exception;
use think\cache\driver\Redis;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\response\Json;
/***
 * 用户控制器
 */
class User extends BaseController
{
    protected $noNeedLogin = ['transferrealauth', 'getmyserviceconfig'];

    /**
     * 个人中心
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function info(): Json
    {
        $memberService = new MemberService();
        return json(api_message($memberService->getInfo($this->request->getMemberId())));
    }


    /**
     * 编辑资料
     * @return Json
     */
    public function save(): Json
    {
        $memberService = new MemberService();
        return json($memberService->saveInfo($this->request->getMemberId(), $this->request->post()));
    }

    /**
     *  系统头像
     * @return Json
     */
    public function hotAvatar(): Json
    {
        $memberService = new MemberService();
        return json($memberService->getHotAvatar());
    }


    /**
     * 更换绑定手机号
     * @return Json
     */
    public function savePhone(): Json
    {
        $memberService = new MemberService();
        return json($memberService->saveBindPhone($this->request->getMemberId(), $this->request->post()));
    }


    /**
     * 实名认证
     * @return Json
     */
    public function realAuth(): Json
    {
        $memberService = new MemberService();
        return json($memberService->realAuth($this->request->getMemberId(), $this->request->post()));
    }

    //防止服务器负载均衡导致发送实名申请无法分配到系统白名单上
    public function transferRealAuth(): Json
    {
        $idCardAuth = new IdcardAuth();
        return json($idCardAuth->idcardAuthVs($_POST['id_card'], $_POST['real_name']));
    }

    /**
     * 获取已认证信息
     * @return Json
     */
    public function certified(): Json
    {
        $memberService = new MemberService();
        return json($memberService->certified($this->request->getMemberId()));
    }

    /**
     * 消息推送
     * @return Json
     */
    public function pushNotice(): Json
    {
        $memberService = new MemberService();
        return json($memberService->pushNotice($this->request->getMemberId(), $this->request->post()));
    }


    /**
     * 注销账号
     * @return Json
     */
    public function disappear()
    {
        $memberService = new MemberService();
        return json($memberService->disappearAccount($this->request->getMemberId(), $this->request->post()));
    }

    /**
     * 取消注销账号
     * @return Json
     * @throws Exception
     */
    public function canalDisappear(): Json
    {
        $memberService = new MemberService();
        return json($memberService->canalDisappearAccount($this->request->getMemberId()));
    }



    /**
     * 我的关注/谁关注我
     * @return Json
     */
    public function myLike(): Json
    {
        $memberService = new MemberService();
        return json($memberService->likeList($this->request->getMemberId(), $this->request->get()));
    }

    /**
     * 站内信列表
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function notice(): Json
    {
        $memberService = new MemberService();
        return json($memberService->noticeList($this->request->getMemberId(), $this->request->get()));
    }

    /**
     * 站内信详情
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function noticeInfo(): Json
    {

        $memberService = new MemberService();
        return json($memberService->noticeInfo($this->request->getMemberId(), $this->request->get('id', 0)));
    }

    /**
     * @return Json
     */
    public function checkSms(): Json
    {
        $authorityService = new AuthorityService();
        return json($authorityService->checkSms($this->request->post()));
    }


    /**
     * 获取本机手机号  验证码
     * @return Json
     */
    public function getUserSms(): Json
    {
        $authorityService = new AuthorityService();
        return json($authorityService->getUserSms($this->request->post()));
    }


    /**
     * 设置青少年模式
     * @return Json
     */
    public function setAdolescent(): Json
    {
        $authorityService = new AuthorityService();
        return json($authorityService->setAdolescent($this->request->getMemberId(), $this->request->post()));
    }

    /**
     * 用户角色列表
     * @return Json
     */
    public function getUserGameRole(): Json
    {
        $memberService = new MemberService();
        return json($memberService->gameRoleList($this->request->getMemberId(), $this->request->post()));
    }


    /**
     * 新增小号
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function addTrumpet(): Json
    {
        $memberService = new MemberService();
        return json($memberService->addTrumpet($this->request->getMemberId(), $this->request->post()));
    }

    /**
     * 小号列表
     * @return Json
     */
    public function trumpetList(): Json
    {
        $memberService = new MemberService();
        $params = $this->request->get();
        return json($memberService->trumpetList($this->request->getMemberId(), $params));
    }


    /**
     * 云挂机小号列表
     * @return Json
     */
    public function yunTrumpetList(): Json
    {
        $memberService = new MemberService();
        return json($memberService->yunTrumpetList($this->request->getMemberId(),  $this->request->get()));
    }



    /**
     * 获取验证码
     */
    public function getCaptcha(): Json
    {
        $redis = new Redis(getRedisConfig());
        //生成随机UID
        $uuid = get_guid_v4();

        //生成图片验证码
        $verify = new \Verify(['length' => 4, 'useCurve' => true]);
        // 验证码图片
        $img = $verify->entry($uuid);
        // 验证码值
        $code = $verify->getCode();

        // 把内容存入 cache，10分钟后过期
        $key = get_guid_v4();
        $redis->set($key, $code, 600);
        // 返回结果
        $data = [
            'captcha_key' => $key,
            'captcha' => "data:image/png;base64," . base64_encode($img)
        ];
        return json(message('请求成功!', true, $data));
    }


    /**
     * Desc : 用户栏的配置
     * User : you name
     * Date : 2024-01-13 14:33
     * @return Json
     */
    public function getMyServiceConfig(): Json
    {
        $params = $this->request->header('Version');
        return json((new MemberService())->getMyServiceConfig($params));
    }


    /**
     * Desc : 实时获取用户最信头像和记录
     * User : you name
     * Date : 2024-01-13 14:33
     * @return Json
     */
    public function getReadInfo(): Json
    {
        $memberService = new MemberService();
        return json($memberService->getReadInfo($this->request->getMemberId()));
    }

}
